/**
 *@Description 服务配置
 *@Author chenxq
 *@Date 201516-01-13
 */
var feeConfigCtrl  = new XFApp.client();
XFApp.client.prototype.feeConfig = {
    successCode : "10000",
    page : null,
    urlItems : {
        queryFeeConfigURL : basePath+"/feeConfig/getConfig",
        getFeeConfigDetailURL : basePath+"/feeConfig/getDetail",
        saveFeeStorageConfigURL : basePath+"/feeConfig/saveFeeStorage",
        saveFeeLogisticsConfigURL : basePath+"/feeConfig/saveFeeLogistics",
        saveFeeReportConfigURL : basePath+"/feeConfig/saveFeeReport",
        serviceConfigURL : basePath+"/serviceConfig/index"
    },
    queryConfig: function(){
        _this = this;
        var customerCode = $("#searchForm input[name='customerCode']").val();
        var username = $("#searchForm input[name='username']").val();
        if(customerCode == '' && username == ''){
            //window.webDialogUtil.alert("提示","请输入查询条件");
            return;
        }
        $.ajax( {
            url:_this.urlItems.queryFeeConfigURL,
            data:$('#searchForm').serialize(),
            type:'get',
            cache:false,
            dataType:'json',
            success:function(data) {
                var result = data.result;
                if(null != result){
                    $("#customerCode").val(result.customerCode);
                    $("#username").val(result.username);
                    $("#customerInfo").show();
                    $("#customerInfo span[name=customerCode]").html(result.customerCode);
                    $("#customerInfo span[name=customerName]").html(result.customerName);
                }else{
                    $("#search-error").text("客户配置信息不存在，请先进行服务配置");
                    return;
                }
                var routes = result.logisticsRoutes || null;
                var reports = result.reportChannels|| null;
                var storages = result.storages|| null;
                if((storages == null || storages.length == 0) && (routes == null || routes.length == 0) && (reports == null||reports.length == 0)){
                    $("#customerInfo span[name=customerCode]").html("");
                    $("#customerInfo span[name=customerName]").html("");
                    $("#customerInfo").hide();
                    webAlert({
                        title:'提示',
                        content:"当前客户还未配置服务，请先进行服务配置！",
                        bg:"gray",
                        width:300,
                        height:10,
                        button:[
                            {
                                name:"确定",
                                callback:function(){
                                    window.location.href = basePath+"/serviceConfig/index?customerCode="+result.customerCode;
                                }
                            }]
                    });
                    return;
                }
                var serviceHtml = "";
                if(storages != null && storages.length > 0){
                    _this.queryItemDetail("storage", storages);
                    serviceHtml += "<label style='width:50px;'>仓储</label>";
                }
                if(routes != null && routes.length > 0){
                    _this.queryItemDetail("route", routes);
                    serviceHtml += "<label style='width:50px;'>物流</label>";
                }
                if(reports != null && reports.length > 0){
                    _this.queryItemDetail("report", reports);
                    serviceHtml += "<label style='width:50px;'>报关</label>";
                }
                $("#serviceList").html(serviceHtml);
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    queryItemDetail:function(feeType, items){
        var optionHtml = "";
        for(var i = 0; i < items.length; i++){
            var item = items[i];
            if("storage" == feeType){
                optionHtml += "<option value='"+item.id+"'>"+item.storageName+"</option>";
            }
            if("route" == feeType){
                optionHtml += "<option value='"+item.id+"'>"+item.routeName+"</option>";
            }
            if("report" == feeType){
                optionHtml += "<option value='"+item.id+"'>"+item.channelName+"</option>";
            }
        }
        $("#"+feeType+"Id").html(optionHtml);
        _this.queryFeeDetail(feeType, items[0]);
        $("#"+feeType+"Id").on("change",function(){
            var itemId = $(this).find("option:selected").val();
            for(var i = 0; i < items.length; i++){
                if(itemId == items[i].id){
                    var item = items[i];
                    _this.queryFeeDetail(feeType,item);
                }
            }
        });
    },
    queryFeeDetail:function(feeType, feeItem){
        var data = {
            feeType: feeType,
            feeId: feeItem.id,
            customerCode: $("#searchForm input[name=customerCode]").val(),
            username: $("#searchForm input[name=username]").val()
        }
        $.ajax({
            url:_this.urlItems.getFeeConfigDetailURL,
            data:data,
            type:'get',
            cache:false,
            dataType:'json',
            success:function(data){
                var feeConfig = data.result;
                _this.setFeeValue(feeType, feeConfig, feeItem)
            }
        });
    },
    setFeeValue: function(feeType,feeConfig, feeItem){
        _this = this;
        if("storage" == feeType){
            var memberType = feeConfig.memberType;
            var storageFee = feeItem.storageFee;
            var inOutFee = feeItem.inOutFee;
            //注意保存之后查询的不一样
            $("#storageConfigForm span[name='goldFee1']").text(storageFee*feeItem.goldRebate.toFixed(2));
            $("#storageConfigForm span[name='goldFee2']").text(inOutFee*feeItem.goldRebate.toFixed(2));
            $("#storageConfigForm span[name='silverFee1']").text(storageFee*feeItem.silverRebate.toFixed(2));
            $("#storageConfigForm span[name='silverFee2']").text(inOutFee*feeItem.silverRebate.toFixed(2));
            $("#storageConfigForm span[name='commonFee1']").text(storageFee.toFixed(2));
            $("#storageConfigForm span[name='commonFee2']").text(inOutFee.toFixed(2));
            $("#storageConfigForm input[name='customFee1']").val(storageFee.toFixed(2));
            $("#storageConfigForm input[name='customFee2']").val(inOutFee.toFixed(2));
            $("#storageConfigForm input[name='storageType']").each(function(){
                var type = $(this).val();
                if(memberType == type){
                    $(this).prop("checked", true);
                }
            });
        }
        if("route" == feeType){
            var memberType = feeConfig.memberType;
            var feeType = feeConfig.feeType;
            var firstFee = (500*feeItem.firstFreight/(feeItem.firstWeight*1000)).toFixed(2);
            var continueFee = (100*feeItem.continueFreight/(feeItem.continueWeight*1000)).toFixed(2);
            $("#routeConfigForm #feeTb0 span[name='goldFee1']").text(firstFee*feeItem.goldRebate.toFixed(2));
            $("#routeConfigForm #feeTb0 span[name='goldFee2']").text(continueFee*feeItem.goldRebate.toFixed(2));
            $("#routeConfigForm #feeTb0 span[name='silverFee1']").text(firstFee*feeItem.silverRebate.toFixed(2));
            $("#routeConfigForm #feeTb0 span[name='silverFee2']").text(continueFee*feeItem.silverRebate.toFixed(2));
            $("#routeConfigForm #feeTb0 span[name='commonFee1']").text(firstFee);
            $("#routeConfigForm #feeTb0 span[name='commonFee2']").text(continueFee);
            $("#routeConfigForm input[name='customFee1']").val(firstFee);
            $("#routeConfigForm input[name='customFee2']").val(continueFee);
            $("#feeTb0 input[name='routeType']").each(function(){
                var type = $(this).val();
                if(memberType == type){
                    $(this).prop("checked", true);
                }
            });
            $("#routeConfigForm #feeTb1 span[name='goldFee1']").text(firstFee*feeItem.goldRebate.toFixed(2));
            $("#routeConfigForm #feeTb1 span[name='silverFee1']").text(firstFee*feeItem.silverRebate.toFixed(2));
            $("#routeConfigForm #feeTb1 span[name='commonFee1']").text(firstFee);
            $("#routeConfigForm input[name='customFee1']").val(firstFee);
            $("#feeTb1 input[name='routeType1']").each(function(){
                var type = $(this).val();
                if(memberType == type){
                    $(this).prop("checked", true);
                }
            });
            if(feeType == 1){
                $('input:radio[name=feeType]')[1].checked = true;
                $("#feeTb1").show();
                $("#feeTb0").hide();
            }else{
                $('input:radio[name=feeType]')[0].checked = true;
                $("#feeTb0").show();
                $("#feeTb1").hide();
            }}

        if("report" == feeType){
            var memberType = feeConfig.memberType;
            var reportFee = feeItem.reportFee;
            $("#reportConfigForm span[name='goldFee1']").text((reportFee*feeItem.goldRebate).toFixed(2));
            $("#reportConfigForm span[name='silverFee1']").text((reportFee*feeItem.silverRebate).toFixed(2));
            $("#reportConfigForm span[name='commonFee1']").text(reportFee.toFixed(2));
            $("#reportConfigForm input[name='customFee1']").val(reportFee.toFixed(2));
            $("#reportConfigForm input[name='reportType']").each(function(){
                var type = $(this).val();
                if(memberType == type){
                    $(this).prop("checked", true);
                }
            });
        }
    },
    save : function(feeType){
        _this = this;
        if("storage" == feeType){
            var memberType = $('input:radio[name=storageType]:checked').val();
            var storageFee, inOutFee;
            if("custom" == memberType){
                storageFee = $("#storageConfigForm input[name=customFee1]").val();
                inOutFee = $("#storageConfigForm input[name=customFee2]").val();
            }else{
                storageFee = $("#storageConfigForm span[name="+memberType+"Fee1]").text();
                inOutFee = $("#storageConfigForm span[name="+memberType+"Fee2]").text();
            }
            var data = {
                customerCode: $("#customerCode").val(),
                username: $("#username").val(),
                storageId:$("#storageId").val(),
                memberType:memberType,
                storageFee:storageFee,
                inOutFee:inOutFee
            }
            $.ajax( {
                url:_this.urlItems.saveFeeStorageConfigURL,
                data:data,
                type:'post',
                cache:false,
                dataType:'json',
                success:function(data) {
                    window.webDialogUtil.alert("提示",data.msg);
                },
                error : function() {
                    window.webDialogUtil.alert("提示","操作失败");
                }
            });
        }
        if("route" == feeType){
            var feeType = $('input:radio[name=feeType]:checked').val();
            var memberType;
            var firstFee, continueFee,commonFee;
            if("1" == feeType){
                memberType = $('input:radio[name=routeType1]:checked').val();
                if("custom" == memberType){
                    commonFee = $("#routeConfigForm input[name=customFee1]").val();
                }else{
                    commonFee = $("#routeConfigForm span[name="+memberType+"Fee1]").text();
                }
                firstFee = continueFee = 0
            }else{
                memberType = $('input:radio[name=routeType]:checked').val();
                if("custom" == memberType){
                    firstFee = $("#routeConfigForm input[name=customFee1]").val();
                    continueFee = $("#routeConfigForm input[name=customFee2]").val();
                }else{
                    firstFee = $("#routeConfigForm span[name="+memberType+"Fee1]").text();
                    continueFee = $("#routeConfigForm span[name="+memberType+"Fee2]").text();
                }
                commonFee = 0;
            }

            var data = {
                customerCode: $("#customerCode").val(),
                username: $("#username").val(),
                routeId:$("#routeId").val(),
                feeType: feeType,
                memberType:memberType,
                firstFee:firstFee,
                continueFee:continueFee,
                commonFee:commonFee
            }
            $.ajax({
                url:_this.urlItems.saveFeeLogisticsConfigURL,
                data:data,
                type:'post',
                cache:false,
                dataType:'json',
                success:function(data) {
                    window.webDialogUtil.alert("提示",data.msg);
                },
                error : function() {
                    window.webDialogUtil.alert("提示","操作失败");
                }
            });
        }
        if("report" == feeType){
            var memberType = $('input:radio[name=reportType]:checked').val();
            var reportFee;
            if("custom" == memberType){
                reportFee = $("#reportConfigForm input[name=customFee1]").val();
            }else{
                reportFee = $("#reportConfigForm span[name="+memberType+"Fee1]").text();
            }
            var data = {
                customerCode: $("#customerCode").val(),
                username: $("#username").val(),
                reportId:$("#reportId").val(),
                memberType:memberType,
                reportFee:reportFee
            }
            $.ajax( {
                url:_this.urlItems.saveFeeReportConfigURL,
                data:data,
                type:'post',
                cache:false,
                dataType:'json',
                success:function(data) {
                    window.webDialogUtil.alert("提示",data.msg);
                },
                error : function() {
                    window.webDialogUtil.alert("提示","操作失败");
                }
            });
        }
    },
    event : function(){
        _this = this;
        $("input[name='feeType']").on("change",function(){
            var itemId = $(this).val();
            if(itemId == 0){
                $("#feeTb0").show();
                $("#feeTb1").hide();
            }
            if(itemId == 1){
                $("#feeTb0").hide();
                $("#feeTb1").show();
            }
        });
        $("#searchButton").on("click",function(){
            feeConfigCtrl.feeConfig.queryConfig();
        });
        $("#saveStorageButton").on("click",function(){
            feeConfigCtrl.feeConfig.save("storage");
        });
        $(".saveRouteButton").on("click",function(){
            feeConfigCtrl.feeConfig.save("route");
        });
        $("#saveReportButton").on("click",function(){
            feeConfigCtrl.feeConfig.save("report");
        });
        feeConfigCtrl.feeConfig.queryConfig();
    }
};

$(function(){
    feeConfigCtrl.feeConfig.event();
});